/* eslint-disable no-undef */
//创建三维视图
var viewerPromise = SippreepViewer.CreateViewer(document.getElementById('viewer1'));


//场景配置
Promise.all([viewerPromise]).then(([viewer]) => {
  document.getElementById('updateScene').onclick = () => {
    var host = document.getElementById('DATABASE').value;
    var sceneID = document.getElementById('SCENEID').value;
    var sceneState = document.getElementById('sceneState');
    sceneState.innerHTML = '正在加载……';
    /**
         * @type { Promise<Sippreep.Viewing.Model> }
         */
    var modelPromise = new Promise((s, f) => {
      if (viewer.model) {
        viewer.unloadModel(viewer.model);
      }
      viewer.loadModel(`${host}/api/UserSpace/ViewFile/${sceneID}?path=/3d.svf`, null, s, f);
    });
    modelPromise.then((model) => {
      return new Promise((s, f) => model.getExternalIdMapping(s, f));
    }).then(e => {
      var objCount = 0;
      for (var key in e) {
        objCount++;
      }
      sceneState.innerHTML = `${objCount}个对象`;
    }).catch((e) => {
      sceneState.innerHTML = JSON.stringify(e);
    });
  };
  document.getElementById('updateScene').onclick();
});
/**
 * @type {Promise<Sippreep.Extensions.EEPTools.EEPToolExtension>}
 */
var toolPromise = viewerPromise.then(v => v.loadExtension('Sippreep.Extensions.EEPTools.EEPToolExtension'));
Promise.all([viewerPromise, toolPromise]).then(([viewer, eeptool]) => {
  _eeptool = eeptool;
  var modeBTs = document.getElementsByClassName('ToolMode');
  for (let i = 0; i < modeBTs.length; i++) {
    modeBTs[i].onclick = (e) => {
      eeptool.set3DCommand(parseInt(e.target.getAttribute('data')));//执行界面按钮的模式选择
    };
  }
  /**
     * 创建一个回调函数
     * 仅在拾取3D点时有效
     * @param {*} point 
     */
  let callback = (point) => {
    console.log(['鼠标点击=', point.x, point.y, point.z]);
  };
    /**
     * 将回调函数传入拾取三维点
     */
  eeptool.addPicked(callback);
  //eeptool.removePicked(callback);

  //订阅视图选中项改变事件
  viewer.addEventListener(Sippreep.Viewing.SELECTION_CHANGED_EVENT, () => {
    var elements = viewer.getSelection();
    document.getElementById('MySelectionValue').innerHTML = elements.length == 1 ? ('ID:' + elements[0]) : (elements.length + '项');
  });
  //设置默认的操作模式
  viewer.addEventListener(Sippreep.Viewing.MODEL_ADDED_EVENT, () => {
    _eeptool.set3DCommand(Sippreep.Extensions.EEPTools.EEPToolCommand.ROTATE);
  });
});